<?php

class Knihovna_Model_Autor
{
    protected  $_table;
    public  function __construct(){
        $this->_table = new Knihovna_Model_DbTable_Autor();
    }
    // fce pro praci s DB tabulkami

    public function najdiAutora($id)
    {
        $id = (int)$id;
        $row = $this->_table->fetchRow('id_autora = ' . $id);

        if (!$row) {
            throw new Exception("Nelze nalezt autora s ID $id");
        }
        return $row->toArray();
    }

    public function pridejAutora($jmeno, $prijmeni, $narozeni)
    {
        $data = array(
            'jmeno' => $jmeno,
            'prijmeni' => $prijmeni,
            'narozeni' => $narozeni,
        );
        $this->_table->insert($data);
        $rid=$this->insert($data);
        $data['rid']=$rid;
        $search = new Knihovna_Model_SearchAutor();
        $search->pridejAutora($data);
    }

    public function upravAutora($id, $jmeno, $prijmeni, $narozeni)
    {
        $data = array(
            'jmeno' => $jmeno,
            'prijmeni' => $prijmeni,
            'narozeni' => $narozeni,
        );
        $this->_table->update($data, 'id_autora =' . (int)$id );
    }

    public function smazAutora($id)
    {
        $this->_table->delete('id_autora =' . (int)$id );
    }

    public function fetchAll(){
        return $this->_table->fetchAll();
    }
}
